package com.wuage.interfaces.dictionary;

import com.wuage.application.IUacDictionaryService;
import com.wuage.application.IUacTenantService;
import com.wuage.domain.model.UacDictionary;
import com.wuage.domain.model.UacTenant;
import com.wuage.infrastructure.security.LoginUser;
import com.wuage.infrastructure.utils.BeanUtils;
import com.wuage.infrastructure.utils.JwtUtils;
import com.wuage.infrastructure.utils.Response;
import com.wuage.interfaces.PagerVO;
import com.wuage.interfaces.dictionary.domain.UacDictionaryDO;
import com.wuage.interfaces.account.domain.DictionaryPageDO;
import com.wuage.interfaces.dictionary.domain.UacDictionaryUpdateDO;
import com.wuage.interfaces.dictionary.vo.UacDictionaryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value = "数据字典相关", tags = "{数据字典}操作相关接口")
@RequestMapping("/dictionary")
public class UacDictionaryController {

    @Resource
    private IUacDictionaryService uacDictionaryService;

    @PostMapping("/")
    @ApiOperation("添加数据字典")
    public Response<Boolean> addDictionary(@RequestBody @Valid UacDictionaryDO uacDictionaryDO) {
        return Response.success(uacDictionaryService.addDictionary(uacDictionaryDO));
    }

    @PutMapping("/")
    @ApiOperation("更新数据字典")
    public Response<Boolean> updateDictionary(@RequestBody @Valid UacDictionaryUpdateDO dictionaryDO) {
        return Response.success(uacDictionaryService.updateById(BeanUtils.copyProperties(dictionaryDO, UacDictionary.class)));
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除数据字典")
    public Response<Boolean> deleteDictionary(@PathVariable(value = "id") Long id) {
        return Response.success(uacDictionaryService.deleteById(id));
    }

    @PostMapping("/classification")
    @ApiOperation("根据类型查询数据字典")
    public Response<PagerVO<UacDictionaryVO>> getByClassification(@RequestBody @Valid DictionaryPageDO pageDO) {
        return Response.success(PagerVO.assembleIPager2PagerVO(uacDictionaryService.getByClassification(pageDO, JwtUtils.getLoginUser().getTenantId()), (List<UacDictionary> list) -> BeanUtils.copyCollections(list, UacDictionaryVO.class)));
    }
}
